//
//  AppDelegate.swift
//  iiatn
//
//  Created by mnine on 18/3/25.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SwiftKeychainWrapper

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        ShareSDK.registerActivePlatforms(
            [
                SSDKPlatformType.typeSinaWeibo.rawValue,
                SSDKPlatformType.typeWechat.rawValue,
                SSDKPlatformType.subTypeWechatTimeline.rawValue,
                SSDKPlatformType.typeQQ.rawValue,
                SSDKPlatformType.subTypeQZone.rawValue
            ],
            onImport: {(platform : SSDKPlatformType) -> Void in
                switch platform
                {
                case SSDKPlatformType.typeSinaWeibo:
                    ShareSDKConnector.connectWeibo(WeiboSDK.classForCoder())
                case SSDKPlatformType.typeWechat:
                    ShareSDKConnector.connectWeChat(WXApi.classForCoder())
                case SSDKPlatformType.subTypeWechatTimeline:
                    ShareSDKConnector.connectWeChat(WXApi.classForCoder())
                case SSDKPlatformType.typeQQ:
                    ShareSDKConnector.connectQQ(QQApiInterface.classForCoder(), tencentOAuthClass: TencentOAuth.classForCoder())
                case SSDKPlatformType.subTypeQZone:
                    ShareSDKConnector.connectQQ(QQApiInterface.classForCoder(), tencentOAuthClass: TencentOAuth.classForCoder())
                default:
                    break
                }
        },
            onConfiguration: {(platform : SSDKPlatformType , appInfo : NSMutableDictionary?) -> Void in
                switch platform
                {
                case SSDKPlatformType.typeSinaWeibo:
                    //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                    appInfo?.ssdkSetupSinaWeibo(byAppKey: "568898243",
                                                appSecret: "38a4f8204cc784f81f9f0daaf31e02e3",
                                                redirectUri: "http://www.sharesdk.cn",
                                                authType: SSDKAuthTypeBoth)
                //设置微信,wechat朋友圈应用信息
                case SSDKPlatformType.typeWechat:
                    appInfo?.ssdkSetupWeChat(byAppId: "wx6c74930366abc826",
                                             appSecret: "9101eb64b875b025bc75a5e6b79377d1")
                case SSDKPlatformType.subTypeWechatTimeline:
                    appInfo?.ssdkSetupWeChat(byAppId: "wx6c74930366abc826",
                                             appSecret: "9101eb64b875b025bc75a5e6b79377d1")
                //设置QQ、qzone应用信息
                case SSDKPlatformType.typeQQ:
                    appInfo?.ssdkSetupQQ(byAppId: "101455078",
                                         appKey: "8c6b0c6cbff68e098aa5e7e204ce4d7f",
                                         authType: SSDKAuthTypeWeb)
                case SSDKPlatformType.subTypeQZone:
                    appInfo?.ssdkSetupQQ(byAppId: "101455078",
                                         appKey: "8c6b0c6cbff68e098aa5e7e204ce4d7f",
                                         authType: SSDKAuthTypeWeb)
//                case SSDKPlatformType.subTypeQZone:
//                    appInfo?.ssdkSetupQQ(byAppId: "100371282",
//                                         appKey: "aed9b0303e3ed1e27bae87c33761161d",
//                                         authType: SSDKAuthTypeWeb)
                default:
                    break
                }
        })
        // 检测用户是否成功登录过，或者成功注册过
        let retrievedBool: Bool? = KeychainWrapper.standard.bool(forKey: "rloginsuccess")
        
        if retrievedBool != nil {
            if retrievedBool! {
                whaslogin = KeychainWrapper.standard.bool(forKey: "rloginsuccess")
                wname = KeychainWrapper.standard.string(forKey: "rname")
                wuserid = KeychainWrapper.standard.integer(forKey: "ruserid")
                print("成功登录或注册过",wuserid ?? 0)
            }
        }
        
        // window 设置
        window = UIWindow(frame: UIScreen.main.bounds)
        let tabBarVC = itMainViewController()
        let leftVC = LeftMenuViewController()
        window?.rootViewController = DrawerViewController(mainVC: tabBarVC, leftMenuVC: leftVC, leftWidth: 300)
        window?.makeKeyAndVisible()

        return true
    }


}

